package com.liuxinlong.jobs;

import com.liuxinlong.api.WeChatMessageInterface;
import com.liuxinlong.constant.TimeFormatConstants;
import com.liuxinlong.enums.MessageContentTypeEnum;
import com.liuxinlong.enums.MessagePushBusinessEnum;
import com.liuxinlong.modules.dao.PatentDepartmentDao;
import com.liuxinlong.modules.dao.PatentProposalDao;
import com.liuxinlong.modules.dao.ProjectDao;
import com.liuxinlong.modules.entity.MessageConfiguration;
import com.liuxinlong.modules.entity.MessagePushRecord;
import com.liuxinlong.modules.entity.PatentDepartment;
import com.liuxinlong.modules.entity.PatentProposal;
import com.liuxinlong.modules.entity.Project;
import com.liuxinlong.utils.ObjectUtils;
import com.liuxinlong.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 风险专利监控提醒定时任务
 */
@Service
@Slf4j
@Component("checkPatentProposalTask")
public class CheckPatentProposalTask {

    @Autowired
    private WeChatMessageInterface weChatMessageInterface;

    @Autowired
    private PatentProposalDao patentProposalDao;

    @Autowired
    private ProjectDao projectDao;

    @Autowired
    private PatentDepartmentDao patentDepartmentDao;

    /**
     * 业务逻辑
     * 存在监控中专利，发送提醒
     */
    public void reptilian() {
        log.info("checkPatentProposalTask start...");
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
        String currentDate = currentTime.substring(0,10);
        String firstNum = currentDate.substring(9,10);
        if (!StringUtils.equals("01",firstNum)) {
            log.info("current date is not first day.");
            log.info("checkPatentProposalTask end...");
            return;
        }
        Map<String,Object> queryParam = new HashMap<>();
        queryParam.put("monitor",1);
        queryParam.put("startNum",0);
        queryParam.put("pageSize",10000);
        List<PatentProposal> patentProposalList = patentProposalDao.pagePatentProposalList(queryParam);
        List<String> targetList = new ArrayList<>();
        if (!patentProposalList.isEmpty()) {
            List<String> projectIdList = patentProposalList.stream().map(PatentProposal::getProjectId).collect(Collectors.toList());
            List<Project> projectList = projectDao.getProjectListByIds(projectIdList);
            List<PatentDepartment> departmentList = patentDepartmentDao.getDepartmentList();
            Map<String,String> depMap = new HashMap<>();
            for (PatentDepartment department :departmentList) {
                depMap.put(department.getId(),department.getDutyUser());
            }
            Set<String> targetSet = new HashSet<>();
            for (Project project : projectList) {
                if(!depMap.containsKey(project.getDepartment())) {
                    continue;
                }
                targetSet.add(depMap.get(project.getDepartment()));
            }
            targetList.addAll(targetSet);
        }
        if (!ObjectUtils.isEmpty(targetList)) {
            String content = "存在监控中的风险专利，请登录处理！";
            MessageConfiguration messageConfiguration = new MessageConfiguration();
            messageConfiguration.setBusinessType(MessagePushBusinessEnum.PATENT_RISK_PENDING.getValue());
            messageConfiguration.setContent(content);
            messageConfiguration.setContentType(MessageContentTypeEnum.TEXT_CARD.getValue());
            MessagePushRecord mpr = weChatMessageInterface.pushMessage(targetList, null, messageConfiguration, currentTime);
        }
        log.info("checkPatentProposalTask end...");
    }
}

